package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.newProjectCenter.Enum.PjCollectionPaymentStatus;
import com.koron.css2.newProjectCenter.bean.PjCollection;
import com.koron.css2.newProjectCenter.bean.query.PjCollectionQuery;
import com.koron.css2.newProjectCenter.mapper.PjCollectionMapper;
import com.koron.css2.newProjectCenter.vo.PjPaymentExportVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 工程收款界面导出功能
 */
public class PjPaymentExport extends AbstractExportExcel {
    /**
     * 缴费方式  用于数据字典取值
     */
    private static final String PAYMENT_METHOD = "BPP";
    private static final String PAYMENT_TYPE = "BPT";
    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        PjCollectionMapper pjCollectionMapper = factory.getMapper(PjCollectionMapper.class);
        OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
        // 1. 获取请求实例
        PjCollectionQuery pjCollectionQuery = JsonUtils.objectToPojo(req.getData(),PjCollectionQuery.class);
        // 3. 查询数据
        List<PjCollection> pjCollectionList = pjCollectionMapper.getList(pjCollectionQuery);
        //  获取缴费方式数据字典
        Map<String, String> paymentMethodMap = Tools.mapDicByCode(factory, PAYMENT_METHOD);
        //获取收款类型数据字典
        Map<String, String> typeNameMap = Tools.mapDicByCode(factory, PAYMENT_TYPE);
        // 查询出系统中的人员信息
        List<OrgUserBean> orgUserList = orgUserMapper.selectList(new OrgUserQueryBean());
        // 将人员信息转成Map
        Map<String, OrgUserBean> orgUserMap = orgUserList.stream().collect(Collectors.toMap(OrgUserBean::getAccount, Function.identity()));
        List<PjPaymentExportVO> pjPaymentExportVOList = new ArrayList<>();
        // 3.1 status->statusName
        for (PjCollection pjCollection:pjCollectionList) {
            String statusName = PjCollectionPaymentStatus.getNameByStatus(pjCollection.getStatus());
            // 收款状态
            pjCollection.setStatusName(statusName);
            // 缴费渠道
            if (pjCollection.getPaymentMethod() != null && paymentMethodMap.containsKey(pjCollection.getPaymentMethod().toString())){
                pjCollection.setPaymentMethodName(paymentMethodMap.get(pjCollection.getPaymentMethod().toString()));
            }
            // 收款员
            if (StringUtils.isNotEmpty(pjCollection.getPaymentStaff())){
                pjCollection.setPaymentStaff(orgUserMap.get(pjCollection.getPaymentStaff()).getUserName());
            }
        }
        pjCollectionList.forEach(pjCollection -> {
            PjPaymentExportVO pjPaymentExportVO = new PjPaymentExportVO();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            pjPaymentExportVO.setStatusName(pjCollection.getStatusName());
            pjPaymentExportVO.setProjectCode(pjCollection.getProjectCode());
            pjPaymentExportVO.setProjectName(pjCollection.getProjectName());
            pjPaymentExportVO.setProjectAddress(pjCollection.getProjectAddress());
            pjPaymentExportVO.setClientName(pjCollection.getClientName());
            if(pjCollection.getProjectDate()!=null){
                pjPaymentExportVO.setProjectDate(formatter.format(pjCollection.getProjectDate()));
            }
            pjPaymentExportVO.setCreateTime(pjCollection.getCreateTime());
            if(pjCollection.getPaymentDate()!=null){
            pjPaymentExportVO.setPaymentDate(pjCollection.getPaymentDate().substring(0,10));
            }
            pjPaymentExportVO.setMoney(pjCollection.getMoney());
            if (pjCollection != null && typeNameMap != null){
                if (pjCollection.getType() != null && typeNameMap.containsKey(pjCollection.getType().toString())){
                    pjPaymentExportVO.setTypeName(typeNameMap.get(pjCollection.getType().toString()));
                }
            }
            pjPaymentExportVO.setConstructionUnit(pjCollection.getConstructionUnit());
            pjPaymentExportVO.setInvoiceUrl(pjCollection.getInvoiceUrl());
            pjPaymentExportVO.setInvoiceNumber(pjCollection.getInvoiceNumber());
            pjPaymentExportVO.setInvoicePerson(pjCollection.getInvoicePerson());
            pjPaymentExportVO.setPaymentStaff(pjCollection.getPaymentStaff());
            if(pjCollection.getInvoiceTime()!=null){
                pjPaymentExportVO.setInvoiceTime(simpleDateFormat.format(pjCollection.getInvoiceTime()));
            }
            pjPaymentExportVOList.add(pjPaymentExportVO);
        });
        AttachmentDto attachment = new AttachmentDto();
        attachment.setBtlTemplate("PjPaymentExport.btl");
        attachment.setFileTemplate("PjPaymentExport.xlsx");
        attachment.setData(pjPaymentExportVOList);
        attachment.setFilename("工程收款-导出");

        attachment.setFileType("excel");
        return attachment;
    }
}
